<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'models/m_select.php';

/**
 * Модель выборки персон
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class M_Select_Persons extends M_Select {
   
   /**
    * Букафка для поиска
    *
    * @var string
    */
   var $_char = '';
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return M_Select_Persons
    */
   function M_Select_Persons() {
      parent::M_Select();
      $this->load->language('select_persons');
   } // end M_Select_Persons
   
   /**
    * Инициализация модельки
    *
    */
   function initialize() {
      // Устанавливаем запрос
      $sql = "
         SELECT
            p.id_person AS id,
            p.title,
            p.slug,
            (
               SELECT
                  SUM(IF(v.status = 'active', 1, 0))
               FROM
                  " . $this->db->dbprefix('video_directors') . " vp
                     INNER JOIN " . $this->db->dbprefix('videos') . " v ON (v.id_video = vp.id_video AND v.status <> 'deleted')
               WHERE
                  vp.id_person = p.id_person
               LIMIT
                  1
            ) AS director_videos,
            (
               SELECT
                  SUM(IF(v.status = 'active', 1, 0))
               FROM
                  " . $this->db->dbprefix('video_actors') . " vp
                     INNER JOIN " . $this->db->dbprefix('videos') . " v ON (v.id_video = vp.id_video AND v.status <> 'deleted')
               WHERE
                  vp.id_person = p.id_person
               LIMIT
                  1
            ) AS actor_videos
         FROM
            " . $this->db->dbprefix('persons') . " p
		 WHERE 
			p.id_person <> 0 
      ";
      if (!empty($this->_char)) {
         $sql .= "
            AND
               p.title LIKE '" . $this->db->escape_str($this->_char) . "%'
         ";
      }
      parent::set_sql($sql);
   }
   
   /**
    * Установка букафки
    *
    * @param string $char
    */
   function set_char($char) {
      $this->_char = $char;
   }
   
   
   /**
    * Обработка результата
    *
    * @access protected
    * @param array $result
    */
   function _process_result(&$result) {
      $result['title'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $result['title']);
   } // end _process_result
   
}
